import { h, Component } from 'preact';
import { connect } from 'preact-redux';
import { Router } from 'preact-router';
import '~/style/global.common';
import history from '~/core/history';

import Iframe from '~/containers/Iframe';
import Luck from '~/containers/Luck';

import { bindActionCreators } from 'redux';
import { setRuntimeVariable } from '~/actions/user';

import { appId } from '~/config.js';
import { jsSDK } from '~/servicer/index.js';
import wechat, { isWeChat, wechatShare } from '~/core/utils/shareWechat';

class App extends Component {
	/** Gets fired when the route changes.
	 *	@param {Object} event		"change" event from [preact-router](http://git.io/preact-router)
	 *	@param {string} event.url	The newly routed URL
	 */

	componentWillMount() {
		// jsSDK({
		// 	url: window.location.href.split('#')[0],
		// 	appId
		// }).then(res => {
		// 	const { appId, timestamp, nonceStr,	signature } = res.body;
		// 	wechat({appId, timestamp, nonceStr,	signature}).then(() => {
		// 		window.console.log('success');
		// 		this.props.setStore({name: 'wechatCongfig', value: true});
		// 		window.wx.ready(() => {
		// 			window.wx.hideMenuItems({
		// 				menuList: [
		// 					'menuItem:share:qq',
		// 					'menuItem:share:weiboApp',
		// 					'menuItem:share:facebook',
		// 					'menuItem:share:QZone',
		// 					'menuItem:openWithQQBrowser',
		// 					'menuItem:openWithSafari',
		// 					'menuItem:share:appMessage',
		// 					'menuItem:share:timeline',
		// 					'menuItem:copyUrl'
		// 				] // 要隐藏的菜单项，只能隐藏“传播类”和“保护类”按钮，所有menu项见附录3
		// 			});
		// 		});
		// 	}).catch((err) => {
		// 		window.console.log('2222', err);
		// 	});
		// });
	}

	handleRoute = e => {
		this.currentUrl = e.url;
	};

	render() {
		return (
			<div id="app">
				<Router onChange={this.handleRoute} history={history}>
					<Iframe path="/iframe" />
					<Luck path="/" />
				</Router>
			</div>
		);
	}
}


function mapStateToProps(state) {
	return state;
}


function mapDispatchToProps(dispatch){
	return bindActionCreators({ setStore: setRuntimeVariable}, dispatch);
}

export default connect(mapStateToProps, mapDispatchToProps)(App);
